package helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

import java.util.Map;

public class SharedPreferencesHelper {

    private Context _context;
    public static String SC_BICEP_CURLS_EXERCISE_INFO = "SC_BICEP_CURLS_EXERCISE_INFO";
    public static String SC_SQUATS_EXERCISE_INFO = "SC_SQUATS_EXERCISE_INFO";
    public static  String SC_SIT_UPS_EXERCISE_INFO = "SC_SIT_UPS_EXERCISE_INFO";
    
    public static String SC_BICEP_CURLS_EXERCISE_DURATION = "SC_BICEP_CURLS_EXERCISE_DURATION";
    public static String SC_SQUATS_EXERCISE_INFO_DURATION = "SC_SQUATS_EXERCISE_DURATION";
    public static String SC_SIT_UPS_EXERCISE_DURATION = "SC_SIT_UPS_EXERCISE_DURATION";
    
    public static String FIRST_APP_START = "FIRST_APP_START";
    private SharedPreferences sharedPreferences;
	

    public SharedPreferencesHelper(Context c) {
        _context = c;
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_context);

        Map<String, ?> map;
        map = sharedPreferences.getAll();
        for (String key : map.keySet()) {
            Log.i("GMM", "key: " + key + " -->" + map.get(key).toString());
        }
    }

    public boolean mustShowExerciseInformation(ExercisesEnum e) {
        try {
            if (e.equals(ExercisesEnum.BICEP_CURLS)) {
                return sharedPreferences.getBoolean(SC_BICEP_CURLS_EXERCISE_INFO, true);
            } else if (e.equals(ExercisesEnum.SQUATS)) {
                return sharedPreferences.getBoolean(SC_SQUATS_EXERCISE_INFO, true);
            } else if (e.equals(ExercisesEnum.SIT_UPS)) {
                return sharedPreferences.getBoolean(SC_SIT_UPS_EXERCISE_INFO, true);
            } else {
                return true;
            }
        } catch (Exception ex) {
            return true;
        }
    }

    private void savePreferences(String key, boolean value) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_context);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private void savePreferences(String key, long value) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_context);
        Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public void savePrefsShowExAgain(ExercisesEnum exType, boolean showAgain) {
        savePreferences(determineExerciseString(exType), showAgain);
    }

    public String determineExerciseString(ExercisesEnum exType) {
        String s = "";
        if (exType.equals(ExercisesEnum.BICEP_CURLS)) {
            s = SC_BICEP_CURLS_EXERCISE_INFO;
        } else if (exType.equals(ExercisesEnum.SQUATS)) {
            s = SC_SQUATS_EXERCISE_INFO;
        }else if(exType.equals(ExercisesEnum.SIT_UPS)){
        	s= SC_SIT_UPS_EXERCISE_INFO;
        }
        return s;
    }

    public void savePrefsExTime(ExercisesEnum exType, long millis) {
        Log.i("GMM","exTIme: "+millis);
        String s = determineExerciseStringDuration(exType);
        long tmp = 0;
        if (sharedPreferences.contains(s)) {
           tmp= sharedPreferences.getLong(s, 0);
        }
        tmp += millis;
        savePreferences(s, tmp);
    }

    private String determineExerciseStringDuration(ExercisesEnum exType) {
        String s = "";
        if (exType.equals(ExercisesEnum.BICEP_CURLS)) {
            s = SC_BICEP_CURLS_EXERCISE_DURATION;
        } else if (exType.equals(ExercisesEnum.SQUATS)) {
            s = SC_SQUATS_EXERCISE_INFO_DURATION;
        } else if(exType.equals(ExercisesEnum.SIT_UPS)){
        	s = SC_SIT_UPS_EXERCISE_DURATION;
        }
        return s;
    }

}
